package wsp.dailymarket.deals;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

/**
 * DealBean
 * @author Matt
 */
public class DealBean {

	private int mID;
	private String mCompany;
	private String mCompanyDisplay;
	private String mShortDesc;
	private String mLongDesc;
	private float mPrice;
	private int mMinQty;
	private int mMaxQty;
	private int mMinUserQty;
	private int mMaxUserQty;
	private boolean mTimed;
	private Timestamp mStartTime;
	private Timestamp mEndTime;
	private int mDefaultImage;
	private int mCategory;
	private String mCategoryName;
	private static final String DATE_FORMAT = "EEEEE, MMMMM d, yyyy h:mm:ss a";
	private static final String DATE_FORMAT_24 = "EEEEE, MMMMM d, yyyy HH:mm:ss";
	private static final long MILLIS_SECOND = 1000;
	private static final long MILLIS_MINUTE = MILLIS_SECOND * 60;
	private static final long MILLIS_HOUR = MILLIS_MINUTE * 60;
	private static final long MILLIS_DAY = 24 * MILLIS_HOUR;
	private static final long MILLIS_WEEK = 7 * MILLIS_DAY;
	private ArrayList<Integer> imageIds;
	private boolean locked;

	public DealBean() {
		mID = 0;
		mCompany = "";
		mCompanyDisplay = "";
		mShortDesc = "";
		mLongDesc = "";
		mPrice = 0;
		mMinQty = 0;
		mMaxQty = -1;
		mMinUserQty = 0;
		mMaxUserQty = -1;
		mTimed = false;
		mStartTime = new Timestamp(System.currentTimeMillis() + MILLIS_HOUR);
		mEndTime = new Timestamp(System.currentTimeMillis() + MILLIS_HOUR + MILLIS_DAY);
		mDefaultImage = 0;
		mCategory = 0;
		mCategoryName = "None";
		imageIds = new ArrayList<Integer>();
		locked = false;
	}

	public DealBean(String company, String companyDisplay) {
		this();
		this.mCompany = company;
		this.mCompanyDisplay = companyDisplay;
	}

	public int getID() {
		return mID;
	}

	public String getCompany() {
		return mCompany;
	}

	public String getCompanyDisplay() {
		return this.mCompanyDisplay;
	}

	public String getShortDesc() {
		return mShortDesc;
	}

	public void setShortDesc(String newValue) {
		this.mShortDesc = newValue;
	}

	public String getLongDesc() {
		return mLongDesc;
	}

	public void setLongDesc(String newValue) {
		this.mLongDesc = newValue;
	}

	public float getPrice() {
		return mPrice;
	}

	public String getPriceDisplay() {
		return NumberFormat.getCurrencyInstance().format(mPrice);
	}

	public void setPrice(float newValue) {
		this.mPrice = newValue;
	}

	public int getMinQty() {
		return this.mMinQty;
	}

	public void setMinQty(int newValue) {
		this.mMinQty = newValue;
	}

	public int getMaxQty() {
		return this.mMaxQty;
	}

	public void setMaxQty(int newValue) {
		this.mMaxQty = newValue;
	}

	public int getMinUserQty() {
		return this.mMinUserQty;
	}

	public void setMinUserQty(int newValue) {
		this.mMinUserQty = newValue;
	}

	public int getMaxUserQty() {
		return this.mMaxUserQty;
	}

	public void setMaxUserQty(int newValue) {
		this.mMaxUserQty = newValue;
	}

	public boolean getTimed() {
		return this.mTimed;
	}

	public void setTimed(boolean newValue) {
		this.mTimed = newValue;
	}

	public Date getStartTime() {
		return this.mStartTime;
	}

	public void setStartTime(Date newValue) {
		this.mStartTime = new Timestamp(newValue.getTime());
	}

	public String getStartTimeDisplay() {
		UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
		Locale locale = viewRoot.getLocale();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, locale);
		return sdf.format(mStartTime);
	}

	public Date getEndTime() {
		return this.mEndTime;
	}

	public void setEndTime(Date newValue) {
		this.mEndTime = new Timestamp(newValue.getTime());
	}

	public String getEndTimeDisplay() {
		UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
		Locale locale = viewRoot.getLocale();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, locale);
		return sdf.format(mEndTime);
	}

	public int getDefaultImage() {
		return this.mDefaultImage;
	}

	public void setDefaultImage(int newValue) {
		this.mDefaultImage = newValue;
	}

	public int getCategory() {
		return this.mCategory;
	}

	public void setCategory(int newValue) {
		this.mCategory = newValue;
	}

	public String getCategoryName() {
		return this.mCategoryName;
	}

	public String getTimeLeftDisplay() {
		if (this.mEndTime == null) {
			return "";
		}
		long timeLeft = this.mEndTime.getTime() - System.currentTimeMillis();
		if (timeLeft < 0) {
			return "Ended";
		} else {
			int num_fields_displayed = 0;
			String display = "";
			if (timeLeft > MILLIS_WEEK) {
				long weeks = timeLeft / MILLIS_WEEK;
				timeLeft -= weeks * MILLIS_WEEK;
				display += weeks + "w ";
				num_fields_displayed++;
			}
			if (timeLeft > MILLIS_DAY) {
				long days = timeLeft / MILLIS_DAY;
				timeLeft -= days * MILLIS_DAY;
				display += days + "d ";
				num_fields_displayed++;
			}
			if (num_fields_displayed < 2 && timeLeft > MILLIS_HOUR) {
				long hours = timeLeft / MILLIS_HOUR;
				timeLeft -= hours * MILLIS_HOUR;
				display += hours + "h ";
				num_fields_displayed++;
			}
			if (num_fields_displayed < 2 && timeLeft > MILLIS_MINUTE) {
				long minutes = timeLeft / MILLIS_MINUTE;
				timeLeft -= minutes * MILLIS_MINUTE;
				display += minutes + "m ";
				num_fields_displayed++;
			}
			if (num_fields_displayed < 2 && timeLeft > MILLIS_SECOND) {
				long seconds = timeLeft / MILLIS_SECOND;
				timeLeft -= seconds * MILLIS_SECOND;
				display += seconds + "s ";
				num_fields_displayed++;
			}
			return display;
		}
	}

	public String getTimeLeftStyle() {
		if (this.mEndTime == null) {
			return "";
		}
		long timeLeft = this.mEndTime.getTime() - System.currentTimeMillis();
		if (timeLeft < MILLIS_HOUR) {
			return "color: #ff0000; font-weight: bold;";
		} else {
			return "font-weight: bold;";
		}
	}

	public String findDefaultImageUrl(int size) {
		return this.findImageUrl(this.mDefaultImage, size);
	}

	public String getStatusMessage() {
		Date now = new Date(System.currentTimeMillis());
		if (now.before(this.getStartTime())) {
			return "Not Started";
		}
		if (this.mTimed && now.after(this.getEndTime())) {
			return "Ended";
		}
		return "In Progress";
	}

	public String getStatusStyle() {
		Date now = new Date(System.currentTimeMillis());
		if (now.before(this.getStartTime())) {
			return "deal-status warn";
		}
		if (this.mTimed && now.after(this.getEndTime())) {
			return "deal-status error";
		}
		return "deal-status good";
	}

	public String getStatusImage() {
		Date now = new Date(System.currentTimeMillis());
		if (now.before(this.getStartTime())) {
			return "clock_pause.png";
		}
		if (this.mTimed && now.after(this.getEndTime())) {
			return "clock_stop.png";
		}
		return "clock_play.png";
	}

	public void populate(int id,
			String company,
			String companyDisplay,
			String shortDesc,
			String longDesc,
			float unitPrice,
			int minQty,
			int maxQty,
			int minUserQty,
			int maxUserQty,
			boolean timed,
			Timestamp startTime,
			Timestamp endTime,
			int defaultImage,
			int category,
			String categoryName) {
		mID = id;
		mCompany = company;
		mCompanyDisplay = companyDisplay;
		mShortDesc = shortDesc;
		mLongDesc = longDesc;
		mPrice = unitPrice;
		mMinQty = minQty;
		mMaxQty = maxQty;
		mMinUserQty = minUserQty;
		mMaxUserQty = maxUserQty;
		mTimed = timed;
		mStartTime = startTime;
		mEndTime = endTime;
		mDefaultImage = defaultImage;
		mCategory = category;
		mCategoryName = categoryName;
	}

	public void populateImages(Connection conn) throws SQLException {
		Statement images_stmt = conn.createStatement();
		ResultSet images_rs = images_stmt.executeQuery("SELECT ID,DEALID FROM APP.DEAL_IMAGES WHERE DEALID = " + this.mID);
		while (images_rs.next()) {
			int imgId = images_rs.getInt("ID");
			if (imgId == this.mDefaultImage) {
				this.imageIds.add(0, imgId);
			} else {
				this.imageIds.add(imgId);
			}
		}
		if (this.imageIds.size() < 1) {
			this.imageIds.add(this.mDefaultImage);
		}
	}

	public ArrayList<Integer> getImages() {
		return this.imageIds;
	}

	public String findImageUrl(int id, int size) {
		String url = "/images/";
		if (size > 0) {
			url += size + "/";
		}
		url += id + ".png";
		return FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + url;
	}

	public String findDealUrl(int id) {
		return "deal.do?deal=" + id;
	}

	public boolean isPopulated() {
		return this.mID > 0;
	}

	public boolean isLocked() {
		return this.locked;
	}

	public void setLocked(boolean newValue) {
		this.locked = newValue;
	}
}
